// pages/chuku/chuku.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    array: ['--请选择--',],
    index: 0,
    classify_id:[0],
    array2: ['--请选择--',],
    index2: 0,
    num:[0,],
    whid:0,
    quantity:'',
    goods_id:[],
  },
  
  bindInput:function(){
    wx.showToast({
      title: '功能暂未开放',
      icon: 'error',
      duration: 1500,
    })
  },
  bindPickerChange: function(e) {
    
    this.setData({
      index: e.detail.value,
      index2:0
    })
    if(this.data.classify_id[this.data.index] != 0){
        this.goods(this.data.classify_id[this.data.index])
    }else{
      this.setData({array2: ['--请选择--',],})
    }
    
    
  },
  goods(e){
    console.log(e)
      wx.request({
        url: 'http://39.106.9.207:8080/warehouse/GetGoods',
        data: {
          whid:this.data.whid,
          typeid:e
        },
        header: {
          'content-type': 'multipart/form-data' // 默认值
        },
        success :(res)=>{
          console.log(res.data);
          let array2 = ['--请选择--',];
          let num =[0,];
          let goods_id = [];
          var i=0;
          while (i<res.data.length) {
            // console.log(res.data[i].name)
            array2.push(res.data[i].name);
            num.push(res.data[i].goodsNum);
            goods_id.push(res.data[i].id);
            i++;
          }
          this.setData({
            array2:[],
            array2:array2,
            num:[],
            num,
            goods_id:[],
            goods_id,
          })
        }
      })
    },
    quantity:function(e){
      this.setData({
        quantity: e.detail.value
      })
      
    },
    submit:function(){
      let reg = /^[0-9]*$/;
      let quantity =this.data.quantity;
      console.log(this.data.num[this.data.index2]);
      // console.log(reg.test(quantity))
      console.log(quantity);
      if(this.data.index ==0){
        wx.showToast({
          title: '请选择分类',
          icon: 'error',
          duration: 1500
        })
      }else if(this.data.index2 ==0){
        wx.showToast({
          title: '请选择物品',
          icon: 'error',
          duration: 1500
        })
      }else if(reg.test(quantity) == false ){
        wx.showToast({
          title: '请输入数字',
          icon: 'error',
          duration: 1000
        })
        this.setData({quantity:''})
      }else if(!quantity == true){
        wx.showToast({
          title: '不能为空',
          icon: 'error',
          duration: 1500
        })
      }else if(quantity.length > 3){
        wx.showToast({
          title: '不能超过三位数',
          icon: 'error',
          duration: 1500
        })
      }else if(quantity > this.data.num[this.data.index2] ){
        wx.showToast({
          title: '不能大于原数量',
          icon: 'error',
          duration: 1500
        })
      }
      else{
        let goods_id = this.data.goods_id[this.data.index2-1]
        console.log(goods_id)
        
        wx.request({
          url: 'http://39.106.9.207:8080/warehouse/UpdateDel',
          data: {
            goodsid: goods_id,
            num: quantity,
          },
          method: 'POST',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          success: (res) => {
            if(res.data == '出库完毕'){
              wx.showToast({
                title: '出库成功',
                icon: 'success',
                duration: 1500
              })
              this.setData({
                index:0,
                index2:0,
                quantity:'',
              })
            }
          }
        })
      }
    },
  bindPicker: function(e) {
    this.setData({
      index2: e.detail.value
    })
  },
  fanhui:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onLoad: function (options) {
    let whid = wx.getStorageSync('warehouseId')
    if (whid) {
      this.setData({
        whid,
      })
    }
    wx.request({
      url: 'http://39.106.9.207:8080/warehouse/GetType', //仅为示例，并非真实的接口地址
      data: {
        whid:this.data.whid,
      },
      header: {
        'content-type': 'multipart/form-data' // 默认值
      },
      success :(res)=>{
        console.log(res.data);
        let array = ['--请选择--',];
        let classify_id = [0];
        var i=0;
        while (i<res.data.length) {
          // console.log(res.data[i].name)
          array.push(res.data[i].name)
          classify_id.push(res.data[i].id)
          i++;
        }
        this.setData({
          array:array,
          classify_id
        })
        // console.log(this.data.classify_id)
      }
    })
  },
})